package com.neusoft.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Deliveryaddress;
import com.neusoft.service.UserAddressService;

//@CrossOrigin("*")   //跨域处理
@RefreshScope
@RestController
@RequestMapping("/UserAddressController")
public class UserAddressController {
	@Autowired
	UserAddressService userAddressService;
	
	@GetMapping("/listDeliveryAddressByUserId/{userId}")
	public CommonResult<List<Deliveryaddress>> listDeliveryAddressByUserId(@PathVariable("userId") String userId)
	{
		return new CommonResult<List<Deliveryaddress>>(200,"success",userAddressService.listDeliveryAddressByUserId(userId));
	}
	
	@GetMapping("/getDeliveryAddressById/{daId}")
	public CommonResult<Deliveryaddress> getDeliveryAddressById(@PathVariable("daId") int daId)
	{
		return new CommonResult<Deliveryaddress>(200,"success",userAddressService.getDeliveryAddressById(daId));
	}
	
	@PostMapping("/saveDeliveryAddress/{contactName}/{contactSex}/{contactTel}/{address}/{userId}")
	public CommonResult<Integer> saveDeliveryAddress(@PathVariable("contactName") String contactName,
			@PathVariable("contactSex") int contactSex,
			@PathVariable("contactTel") String contactTel,
			@PathVariable("address") String address,
			@PathVariable("userId") String userId
			)
	{
		Deliveryaddress deliveryaddress=new Deliveryaddress();
		deliveryaddress.setContactName(contactName);
		deliveryaddress.setContactSex(contactSex);
		deliveryaddress.setContactTel(contactTel);
		deliveryaddress.setAddress(address);
		deliveryaddress.setUserId(userId);
		return new CommonResult<Integer>(200,"success",userAddressService.saveDeliveryAddress(deliveryaddress));
	}
	
	@PutMapping("/updateDeliveryAddress/{contactName}/{contactSex}/{contactTel}/{address}/{userId}/{daId}")
	public CommonResult<Integer> updateDeliveryAddress(
			@PathVariable("contactName") String contactName,
			@PathVariable("contactSex") int contactSex,
			@PathVariable("contactTel") String contactTel,
			@PathVariable("address") String address,
			@PathVariable("userId") String userId,
			@PathVariable("daId") int daId){
		Deliveryaddress deliveryaddress=new Deliveryaddress();
		deliveryaddress.setContactName(contactName);
		deliveryaddress.setContactSex(contactSex);
		deliveryaddress.setContactTel(contactTel);
		deliveryaddress.setAddress(address);
		deliveryaddress.setUserId(userId);
		deliveryaddress.setDaId(daId);
		return new CommonResult<Integer>(200,"success",userAddressService.updateDeliveryAddress(deliveryaddress));
	}
	
	@DeleteMapping("/removeDeliveryAddress/{daId}")
	public  CommonResult<Integer> removeDeliveryAddress(@PathVariable("daId")int daId){
		return new CommonResult<Integer>(200,"success",userAddressService.removeDeliveryAddress(daId));
	}
	
}
